import qs from 'qs';
import Request from './request';

/**
 * post请求可能有query，统一拼接方法
 * @param path  {string}            请求地址
 * @param params {object}           请求参数
 * @property data {object}          post请求data
 * @property param {object}         get请求param
 */
function paramsQuery(path, params) {
    let request = Object.assign({data: {}, param: {}}, params);
    let param = qs.stringify(request.param);
    if (param) {
        path = `${path}?${param}`;
    }
    return {
        data: request.data,
        path
    };
}

// 登录
export function fetchLogin(params) {
    let ret = paramsQuery('/api/user/external/login', params);
    return Request.post(ret.path, ret.data)
}

// 实时数据/公司
export function fetchRealCompany(params) {
    let ret = paramsQuery('/cpa/api/channel/get_my_channel', params);
    return Request.get(ret.path, {})
}

// 实时数据/产品
export function fetchRealProduct(params) {
    let ret = paramsQuery('/cpa/api/channel_product/list_product', params);
    return Request.get(ret.path, ret.param)
}

// 实时数据列表/查询
export function fetchRealSearch(params) {
    let ret = paramsQuery('/cpa/api/realtime/search_by_channel', params);
    return Request.get(ret.path, ret.param)
}
